package com.youjiang.meeting.utils.weixin;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import com.youjiang.meeting.utils.ToolUtils;
import com.youjiang.meeting.utils.weixinbean.AccessToken;

import net.sf.json.JSONObject;



public class WeixinButtonUtil {
	private static String url="https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN";
	public static JSONObject updataWeixinButton(String content,String realPath,String appid,String appsecrect) throws Exception{
		AccessToken accessToken=WeixinUtil.getAddreAccessToken(appid,appsecrect);
		String toUrl=url.replace("ACCESS_TOKEN", accessToken.getToken());
		createButton(realPath,content);
		return WeixinUtil.doPostStr(toUrl, content);
	}
	private  static void createButton(String realPath,String content) throws FileNotFoundException, IOException{
		File realFile=new File(realPath);
		if(!realFile.exists()){
			realFile.exists();
		}
		File file =new File(realPath,"button.myobj");
		ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream(file));
		out.writeObject(content);
		out.close();
	}
	
	public static String getButton(String realPath) throws FileNotFoundException, IOException, ClassNotFoundException{
		File realFile=new File(realPath);
		if(!realFile.exists()){
			return null;
		}
		File file =new File(realPath,"button.myobj");
		if(file.exists()){
			ObjectInputStream in=new ObjectInputStream(new FileInputStream(file));
			String content= (String) in.readObject();
			in.close();
			return content;
		}
		return null;
	}
	
}
